package com.tangyuan.common.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.tangyuan.common.config.AlipayConfig;

public class AlipayUtils {

    /**
     * 统一收单交易创建接口
     */
    private static AlipayClient alipayClient = null;

    /**
     * 获得初始化的AlipayClient
     *
     * @return 支付宝客户端
     */

    public static AlipayClient getAlipayClient() {
        if (alipayClient == null) {
            synchronized (AlipayConfig.class) {
                CertAlipayRequest alipayConfig = new CertAlipayRequest();
                alipayConfig.setPrivateKey(AlipayConfig.APP_PRIVATE_KEY);
                alipayConfig.setServerUrl(AlipayConfig.APP_GATEWAY);
                alipayConfig.setAppId(AlipayConfig.ALIPAY_APPID);
                alipayConfig.setCharset(AlipayConfig.CHARSET);
                alipayConfig.setSignType(AlipayConfig.SIGNTYPE);
                alipayConfig.setEncryptor("");
                alipayConfig.setFormat("json");
                alipayConfig.setCertPath(AlipayConfig.alipayCertPublicKey);
                alipayConfig.setAlipayPublicCertPath(AlipayConfig.appCertPublicKey);
                alipayConfig.setRootCertPath(AlipayConfig.alipayRootCert);
                if (null == alipayClient) {
                    try {
                        alipayClient = new DefaultAlipayClient(alipayConfig);
                    } catch (AlipayApiException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return alipayClient;
    }
}
